package com.platform.cdcs.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.adapter.ProductItemAdatper;
import com.platform.cdcs.fragment.detail.InOutTicketFragment;
import com.platform.cdcs.fragment.detail.ProductInfoFragment;
import com.platform.cdcs.fragment.stock.EditInputFragment;
import com.platform.cdcs.fragment.stock.EditProductFragment;
import com.platform.cdcs.fragment.stock.ProductDetailFragment;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.MockObj;
import com.platform.cdcs.model.NoticeList;
import com.platform.cdcs.model.ProductList;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.sherchen.slidetoggleheader.views.ObservableXListView;
import com.sherchen.slidetoggleheader.views.XListView;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by holytang on 2017/11/1.
 */
public class GoodsNotifyFragment extends BaseFragment {

    private ProductItemAdatper adatper;
    private ObservableXListView listView;
    private int pageIndex = 1;
    private NoticeList.NoticeItem item;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        item = (NoticeList.NoticeItem) getArguments().getSerializable("model");
    }

    @Override
    public void initView(View view) {
        initLoadImg(view.findViewById(R.id.load));
        adatper = new ProductItemAdatper(getContext());
        adatper.setModel(3);
        hideThisToolBar(view);
        setTitle("到货通知详情");
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        TextView btnView = (TextView) view.findViewById(R.id.button1);
        btnView.setVisibility(View.VISIBLE);
        btnView.setText("一键入库");
        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inStock();
            }
        });
        TextView layout = new TextView(getContext());
        LinearLayout rootLayout = new LinearLayout(getContext());
        rootLayout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        int margin = Utils.convertDIP2PX(getContext(), 10);
        int padding = Utils.convertDIP2PX(getContext(), 12);
        layoutParams.setMargins(margin, margin, margin, margin);
        layout.setPadding(padding, padding, padding, padding);
        layout.setLayoutParams(layoutParams);
        layout.setTextSize(14);
        layout.setBackgroundResource(R.drawable.shape_corner);
        layout.setTextColor(ContextCompat.getColor(getContext(),R.color.text_dark));
        layout.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_right, 0);
        layout.setGravity(Gravity.CENTER_VERTICAL);
        layout.setText("发货方出库单");
        adatper.setItemListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProductList.ProductItem item = (ProductList.ProductItem) view.getTag(R.mipmap.ic_launcher);
                Bundle bundle = new Bundle();
                bundle.putSerializable("model", item);
                bundle.putInt("type", 2);
                FragmentUtil.navigateToInNewActivity(getActivity(), ProductInfoFragment.class, bundle);
            }
        });
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                bundle.putString("id", item.getFileReportId());
                FragmentUtil.navigateToInNewActivity(getActivity(), InOutTicketFragment.class, bundle);
            }
        });
        listView = (ObservableXListView) view.findViewById(android.R.id.list);
        rootLayout.addView(layout);
        listView.addHeaderView(rootLayout);
        listView.setPullRefreshEnable(true);
        listView.setPullLoadEnable(true);
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                adatper.clear();
                adatper.notifyDataSetChanged();
                pageIndex = 1;
                requestList(false);
            }

            @Override
            public void onLoadMore() {
                requestList(false);
            }
        });
        listView.setAdapter(adatper);
        requestList(true);
    }

    @Override
    public int layoutId() {
        return R.layout.listview;
    }

    private void requestList(boolean show) {
        if (show) {
            showLoadImg();
        }
        Map<String, String> map = new HashMap<>();
        map.put("fileReportId", item.getFileReportId());
        map.put("pageIndex", String.valueOf(pageIndex));
        map.put("pageSize", String.valueOf(Constant.PAGE_SIZE));
        getHttpClient().post().url(Constant.DOCUMENT_INFO_LST).params(Constant.makeParam(map)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                listView.stopLoadMore();
                listView.stopRefresh();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    if (getContext() == null) {
                        return;
                    }
                    dismissLoadImg();
                    listView.stopLoadMore();
                    listView.stopRefresh();
                    Type type = new TypeToken<BaseObjResponse<DocumentInfoList>>() {
                    }.getType();
                    BaseObjResponse<DocumentInfoList> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        adatper.addAll(response.getResult().getDocumentInfoList());
                        adatper.notifyDataSetChanged();
                        if (response.getResult().getDocumentInfoList().size() == Constant.PAGE_SIZE) {
                            pageIndex++;
                            listView.setPullLoadEnable(true);
                        } else {
                            listView.setPullLoadEnable(false);
                        }
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {

                }
            }
        });
    }

    private void inStock() {
        //添加到入库列表
        Bundle bundle = new Bundle();
        bundle.putInt("type", 1);
        bundle.putString("id", item.getFileReportId());
        bundle.putString("name", item.getMsgInfo());

        FragmentUtil.navigateToInNewActivity(getActivity(), EditInputFragment.class, bundle);
    }

    private class DocumentInfoList extends MockObj {
        private List<ProductList.ProductItem> documentInfoList;

        public List<ProductList.ProductItem> getDocumentInfoList() {
            return documentInfoList;
        }

        public void setDocumentInfoList(List<ProductList.ProductItem> documentInfoList) {
            this.documentInfoList = documentInfoList;
        }
    }
}
